<template>
  <div class="type-nav">
    <div class="container">
      <div @mouseenter="issortshow = true" @mouseleave="issortshow = false">
        <h2 class="all">全部商品分类</h2>
        <div class="sort" v-show="isSort">
          <div
            class="all-sort-list2"
            @mouseenter="isMouseInCategory = true"
            @mouseleave="mouseLeaveCategory"
            @click="toSearch">
            <div
              class="item"
              v-for="(category1, index) in Category1List"
              :key="category1.id"
              @mouseenter="mouseEnterthrottle(index, category1.id)"
              :class="{ active: index === mouseEnterIndex }">
              <h3
                :data-category1Id="category1.id"
                :data-categoryName="category1.name">
                <a
                  :data-category1Id="category1.id"
                  :data-categoryName="category1.name">
                  {{ category1.name }}
                </a>
              </h3>
              <div class="item-list clearfix">
                <div class="subitem">
                  <dl
                    class="fore"
                    v-for="category2 in category1.children"
                    :key="category2.id">
                    <dt
                      :data-category2Id="category2.id"
                      :data-categoryName="category2.name">
                      <a
                        :data-category2Id="category2.id"
                        :data-categoryName="category2.name">
                        {{ category2.name }}
                      </a>
                    </dt>
                    <dd>
                      <em
                        v-for="category3 in category2.children"
                        :key="category3.id"
                        :data-category3Id="category3.id"
                        :data-categoryName="category3.name">
                        <a
                          :data-category3Id="category3.id"
                          :data-categoryName="category3.name">
                          {{ category3.name }}
                        </a>
                      </em>
                    </dd>
                  </dl>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>

      <nav class="nav">
        <a href="###">服装城</a>
        <a href="###">美妆馆</a>
        <a href="###">尚品汇超市</a>
        <a href="###">全球购</a>
        <a href="###">闪购</a>
        <a href="###">团购</a>
        <a href="###">有趣</a>
        <a href="###">秒杀</a>
      </nav>
    </div>
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
import { throttle } from "lodash";
export default {
  name: "TypeNav",
  data() {
    return {
      /*
        定义一个数据mouseEnterIndex保存当前鼠标移入三级列表的下标
        在item列表中进行判断，如果当前item的下标和mouseEnterIndex想等，则添加active类名 当鼠标移出整个三级列表区域的时候，把这个mouseEnterIndex的值恢复为-1
       */
      mouseEnterIndex: -1,

      /*
        因为mouseEnter事件函数节流后。鼠标移出整个三级列表区域，还会再执行一次
        需要一个数据判断鼠标是否还在三级区域
        如果不在，则mouseEnter事件函数中的核心代码就不再执行了
       */
      isMouseInCategory: false,

      issortshow: false,
    };
  },
  mounted() {
    // 调用请求一级列表数据的方法
    this.getCategory1List();
    this.mouseEnterthrottle = throttle(this.mouseEnter, 200, {
      // 指定调用在节流开始前是否执行一次函数
      leading: false,
      // 指定在调用节流函数后是否执行一次函数（如果为true 鼠标移出来后时间到了还会执行一次函数；如果为false 鼠标在节流时间没到的时候移入某个元素上 函数就会一直不再触发）
      trailing: true,
    });
  },
  computed: {
    // 一级列表的数据
    ...mapState("Category", ["Category1List"]),
    isSort() {
      if (this.$route.name === "Home") return true;
      return this.issortshow;
    },
  },
  methods: {
    // 请求一级列表数据的方法
    ...mapActions("Category", ["getCategory1List", "getCategory2List"]),

    // 鼠标移入事件
    mouseEnter(index, id) {
      if (!this.isMouseInCategory) return;
      this.mouseEnterIndex = index;
      this.getCategory2List(id);
    },

    // 占位 收集throttle处理的节流函数
    mouseEnterthrottle() {},

    // 离开三级分类区域的事件回调函数
    mouseLeaveCategory() {
      // 把保存鼠标所在三级列表的下标恢复为-1
      this.mouseEnterIndex = -1;

      // 把保存鼠标是否在三级列表中的isMouseInCategory改为false
      this.isMouseInCategory = false;
    },
    // 跳转到search页面
    toSearch(e) {
      const { category1id, category2id, category3id, categoryname } =
        e.target.dataset;

      const { params } = this.$route;
      this.$router.push({
        name: "Search",
        query: {
          category1Id: category1id,
          category2Id: category2id,
          category3Id: category3id,
          categoryName: categoryname,
        },
        params,
      });
    },

    // 控制鼠标移入显示与隐藏
    sortshow() {},
  },
  watch: {
    // 侦听路由的改变隐藏导航
    $route() {
      this.issortshow = false;
    },
  },
};
</script>

<style scoped lang="less">
.type-nav {
  border-bottom: 2px solid #e1251b;

  .container {
    width: 1200px;
    margin: 0 auto;
    display: flex;
    position: relative;

    .all {
      width: 210px;
      height: 45px;
      background-color: #e1251b;
      line-height: 45px;
      text-align: center;
      color: #fff;
      font-size: 14px;
      font-weight: bold;
    }

    .nav {
      a {
        height: 45px;
        margin: 0 22px;
        line-height: 45px;
        font-size: 16px;
        color: #333;
      }
    }

    .sort {
      position: absolute;
      left: 0;
      top: 45px;
      width: 210px;
      height: 461px;
      position: absolute;
      background: #fafafa;
      z-index: 999;

      .all-sort-list2 {
        .item {
          h3 {
            line-height: 30px;
            font-size: 14px;
            font-weight: 400;
            overflow: hidden;
            padding: 0 20px;
            margin: 0;
            cursor: pointer;
            a {
              color: #333;
            }
          }

          .item-list {
            display: none;
            position: absolute;
            width: 734px;
            min-height: 460px;
            background: #f7f7f7;
            left: 210px;
            border: 1px solid #ddd;
            top: 0;
            z-index: 9999 !important;

            .subitem {
              float: left;
              width: 650px;
              padding: 0 4px 0 8px;

              dl {
                border-top: 1px solid #eee;
                padding: 6px 0;
                overflow: hidden;
                zoom: 1;

                &.fore {
                  border-top: 0;
                }

                dt {
                  float: left;
                  width: 54px;
                  line-height: 22px;
                  text-align: right;
                  padding: 3px 6px 0 0;
                  font-weight: 700;
                  cursor: pointer;
                }

                dd {
                  float: left;
                  width: 415px;
                  padding: 3px 0 0;
                  overflow: hidden;

                  em {
                    float: left;
                    height: 14px;
                    line-height: 14px;
                    padding: 0 8px;
                    margin-top: 5px;
                    border-left: 1px solid #ccc;
                    cursor: pointer;
                  }
                }
              }
            }
          }

          /*  &:hover {
            .item-list {
              display: block;
            }
          } */
          &.active {
            h3 {
              background: yellowgreen;
            }
            .item-list {
              display: block;
            }
          }
        }
      }
    }
  }
}
</style>
